﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;

namespace OgrIsleri.DashBoard.Modul.Birimler
{
    public partial class Liste : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            if (Request.QueryString["Guncelle"] != null)
                Guncelle();

            if (Request.QueryString["Sil"] != null)
                Sil();

            VeriDoldur();
        }
        private void VeriDoldur()
        {
            using (DbEntities db = new DbEntities())
            {
                var veri = db.TblBirimlers.ToList();
                RptListe.DataSource = veri;
                RptListe.DataBind();
            }
        }
        private void Guncelle()
        {
            using (DbEntities db = new DbEntities())
            {
                int BirimID = Convert.ToInt32(Request.QueryString["Guncelle"]);
                var veri = db.TblBirimlers.Where(o => o.BirimID == BirimID).SingleOrDefault();
                txtAd.Text = veri.BirimAdi;
                BtnKaydet.Text = "Düzenle";
            }
        }
        private void Sil()
        {
            using (DbEntities db = new DbEntities())
            {
                int BirimID = Convert.ToInt32(Request.QueryString["Sil"]);
                var veri = db.TblBirimlers.Where(o => o.BirimID == BirimID).SingleOrDefault();
                db.TblBirimlers.Remove(veri);
                db.SaveChanges();
            }
            Response.Redirect("Liste.aspx");
        }
        protected void BtnKaydet_Click(object sender, EventArgs e)
        {
            using (DbEntities db = new DbEntities())
            {
                if (Request.QueryString["Guncelle"] != null)
                {
                    int BirimID=Convert.ToInt32(Request.QueryString["Guncelle"]);
                    var veri = db.TblBirimlers.Where(o => o.BirimID == BirimID).SingleOrDefault();
                    veri.BirimAdi = txtAd.Text;
                    db.SaveChanges();
                }
                else
                {
                    var data = new TblBirimler();
                    data.BirimAdi = txtAd.Text;
                    db.TblBirimlers.Add(data);
                    db.SaveChanges();
                }
            }
            Response.Redirect("Liste.aspx");
        }
    }
}